//
//  HMOptinalView.m
//  美团
//
//  Created by Shenzhen_iOS_07 on 16/10/10.
//  Copyright © 2016年 Shenzhen_iOS_07. All rights reserved.
//

#import "HMOptinalView.h"
#import "UIButton+HMUtilities.h"
#import "UIColor+HMUtilities.h"
#import "UIColor+CZAddition.h"
#import "UIView+ZUtility.h"

@interface HMOptinalView ()

/* 按钮 */
@property (strong, nonatomic) NSMutableArray <UIButton *> *buttons;

/* 线条 */
@property (strong, nonatomic) UIView *lineView;

@end

@implementation HMOptinalView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self hmSetupSubviews];
        
    }
    return self;
}

- (void)hmSetupSubviews
{
    
    /*------ 添加三个按钮 ------*/
    NSArray *names = @[@"接诊条件", @"医生简介", @"就诊时间"];
    int index = 0;
    for (NSString *name in names) {
        UIButton *btn = [UIButton buttonWithTitle:name textColor:[UIColor grayColor] andSize:17];
        
        btn.backgroundColor = [UIColor whiteColor];
        
        UIView *spaceLine = [[UIView alloc]init];
        spaceLine.backgroundColor = [UIColor cz_colorWithHex:0xd7d7d7];
        
        [btn addSubview:spaceLine];
        
        [spaceLine mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.right.equalTo(btn);
            make.top.equalTo(btn).offset(5);
            make.bottom.equalTo(btn).offset(-5);
            make.centerY.equalTo(btn);
            make.width.mas_equalTo(1);
        }];
        
        
        [btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = index++;
        
        [self addSubview:btn];
        [self.buttons addObject:btn];
    }
    
    // 约束
    [self.buttons[0] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(self);
    }];
    
    [self.buttons[1] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.top.mas_equalTo(self.buttons[0]);
        make.left.mas_equalTo(self.buttons[0].mas_right);
    }];
    
    [self.buttons[2] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.top.mas_equalTo(self.buttons[1]);
        make.left.mas_equalTo(self.buttons[1].mas_right);
        make.right.mas_equalTo(self.mas_right);
    }];
    
    /*------ 添加底部的横线 ------*/
    UIView *lineView = [[UIView alloc] init];
    [lineView setBackgroundColor:[UIColor cz_colorWithHex:0x13c4c4]];
    [self addSubview:lineView];
    _lineView = lineView;
    
    // 约束
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.bottom.mas_equalTo(self.buttons[0]);
        make.height.mas_equalTo(2);
    }];
}

- (void)selectIndex:(NSUInteger)index
{
//    [self buttonAction:self.buttons[index]];
    
    CGFloat x = self.buttons[index].x;
    
    [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(x);
    }];
    
    [UIView animateWithDuration:0.25 animations:^{
        // 立即刷新界面
        [self layoutIfNeeded];
    }];
}

#pragma mark - 按钮响应事件

- (void)buttonAction:(UIButton *)sender
{
    /*------ 1. 移动横线 ------*/
    CGFloat x = self.buttons[sender.tag].x;
    
    [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(x);
    }];
    
    [UIView animateWithDuration:0.25 animations:^{
        // 立即刷新界面
        [self layoutIfNeeded];
    }];
    
    // 触发代理
    if ([self.delegate respondsToSelector:@selector(optionalView:didSelectedIndex:)]) {
        [self.delegate optionalView:self didSelectedIndex:sender.tag];
    }
}

#pragma mark - Getter & Setter

- (NSMutableArray <UIButton *> *)buttons
{
    if (_buttons == nil) {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}

@end
